import type {SysDictTypeAdd, SysDictTypeData} from "@/pages/system/dict/type/type";
import type {ReactNode} from "react";
import type {UseFetchDataAction} from "@ant-design/pro-table/es/typing";
import {Button, message, Space, Tag} from "antd";
import type {ProColumns} from "@ant-design/pro-table";
import type {OptionActions} from "@/type/type";
import DictTypeAdd from "@/pages/system/dict/type/list/components/DictTypeAdd";
import {history} from "umi";
import {createEnum} from "@/services/gen/api";


// @ts-ignore
const columns: ProColumns<SysDictTypeData> = (optionAction: OptionActions<SysDictTypeAdd>) => {
  return [
    {
      dataIndex: "index",
      valueType: "index",
    },
    {
      title: "字典名称",
      dataIndex: "name",
      align: 'center',
      render: (text: ReactNode, record: SysDictTypeData, index: number, action: UseFetchDataAction<SysDictTypeData>) => {
        return (
          <a type="link" onClick={() => {
            history.push({
              pathname: "/system/manager/dict/value",
              query: {
                dictTypeId: record.id as any,
                enum_: record.enum_ as any
              }
            })
          }}>{text}</a>
        )
      }
    },
    {
      title: "字典描述",
      dataIndex: "desc",
      align: 'center',
      hideInSearch: true
    },
    {
      title: "枚举",
      dataIndex: "enum_",
      align: 'center',
      hideInSearch: true,
      render: (text: ReactNode, record: SysDictTypeData, index: number, action: UseFetchDataAction<SysDictTypeData>) => {
        return record.enum_ ?
          (<Tag color="green">是</Tag>) :
          (<Tag color="yellow">否</Tag>)
      }
    },
    {
      title: "状态",
      dataIndex: "status",
      align: 'center',
      hideInSearch: true,
      render: (text: ReactNode, record: SysDictTypeData, index: number, action: UseFetchDataAction<SysDictTypeData>) => {
        const editStatus = (record: SysDictTypeData) => {
          optionAction.edit({
            ...record,
            status: !record.status
          })
        }
        return record.status ?
          (<Tag color="green" onClick={() => editStatus(record)}>启用</Tag>) :
          (<Tag color="yellow" onClick={() => editStatus(record)}>停用</Tag>)
      }
    },
    {
      title: "创建时间",
      dataIndex: "createTime",
      align: 'center',
      hideInSearch: true,
    },
    {
      title: "更新时间",
      dataIndex: "updateTime",
      align: 'center',
      hideInSearch: true
    },
    {
      title: "操作",
      valueType: 'option',
      align: "center",
      key: "option",
      width: "200px",
      render: (text: ReactNode, record: SysDictTypeData, index: number, action: UseFetchDataAction<SysDictTypeData>) => {
        return (
          <Space>
            <DictTypeAdd edit={true} initialValues={record}
                         title={"编辑字典"} {...optionAction.editModal(record)}/>
            {
              record.enum_ && <Button type="link" onClick={() => {
                // @ts-ignore
                createEnum(record.id).then(res => {
                  message.success("创建成功").then()
                })
              }}>创建枚举字段</Button>
            }
            <Button type="link" danger onClick={() => {
              optionAction.remove([record.id]);
            }}>删除</Button>
          </Space>
        )
      }
    }
  ]
}


export default columns;
